﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IniParser;
using IniParser.Model;
//using System.Runtime.InteropServices;

namespace GWIUpdater
{
    public class Ini
    {
        //[DllImport("kernel32.dll")]
        //private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);

        //[DllImport("kernel32.dll")]
        //private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
		private static FileIniDataParser parser=new FileIniDataParser();
        /// <summary>
        /// 读ini配置文件
        /// </summary>
        /// <param name="iniFile">路径</param>
        /// <param name="section">块[]</param>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public static string Read(string iniFile,string section,string key)
        {
//            int valLen = 1024;
//            StringBuilder retVal = new StringBuilder(valLen);
//            GetPrivateProfileString(section, key, "", retVal, valLen, iniFile);
//            return retVal.ToString();
			IniData data = parser.ReadFile (iniFile);
			return data [section] [key];
        }
        /// <summary>
        /// 写ini文件
        /// </summary>
        /// <param name="iniFile"></param>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public static void Write(string iniFile, string section, string key, string val)
        {
            //WritePrivateProfileString(section, key, val, iniFile);
			IniData data = parser.ReadFile (iniFile);
			data [section] [key] = val;
			parser.WriteFile (iniFile,data,Encoding.Default);
        }
    }
}
